package loaders
{
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.LoaderContext;
	
	import memory.MemoryRecovery;
	
	import utils.XmlUtil;
	
	import view.loadings.ILoadingMC;
	
	/**
	 *实现多个数据一起加载，并在界面上显示进度 
	 */
	public class Preloader extends EventDispatcher
	{
		private var loader:QueueLoader;                     //用来加载数据
		private var x:Number=450;                           //loadingMC的坐标X
		private var y:Number=300;                           //loadingMC的坐标Y
		private var text:String="加载数据中...";             //loadingMC加载数据的时候所要显示的文本
		private var loadingMC:*;                            //loadingMC
		private var container:Sprite;                       //存放loadingMC的容器
		/**                         
		 * 初始化
		 * xml示意如下
		 * <xml a="" b="">
		 *    <Url></Url>
		 *    <Url></Url>
		 * </xml>
		 */ 
		public function init(xml:XML,container:Sprite):void
		{
			if(loader==null && xml!=null && xml.Url.length()!=0)
			{
				 loader=new QueueLoader(false,new LoaderContext(true));
				 this.container=container;
				 XmlUtil.setPropertys([this],xml);
				 addItems(xml);
			}else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		//添加所有的加载任务
		private function addItems(xml:XML):void
		{
			 for(var i:int=0;i<xml.Url.length();i++)
			 {
			 	loader.addItem(xml.Url[i]);
			 }
		}
		/**
		 * 开始加载
		 */ 
		public function start():void
		{
			if(loader!=null)
			{
				loader.execute();
				loader.addEventListener(QueueLoaderEvent.QUEUE_PROGRESS,progress_handler);
				loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,on_all_loaded);
				loader.addEventListener(QueueLoaderEvent.ITEM_START,on_item_start);
			}
		}
		private function progress_handler(e:QueueLoaderEvent):void
		{
			if(loadingMC!=null)loadingMC.updateProgress(e.bytesLoaded,e.bytesTotal);
		}
		private function on_all_loaded(e:QueueLoaderEvent):void
		{
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function on_item_start(e:QueueLoaderEvent):void
		{
			var p:RegExp=/\w*.\w*$/;
			var fileName:String="加载"+String(e.path.url.match(p))+"中...";
			if(loadingMC!=null)loadingMC.text=fileName;
		}
		/**
		 * 在界面上显示进度
		 */ 
		public function setLoadingMc(mc:ILoadingMC):void
		{
			loadingMC=mc;
			container.addChild(DisplayObject(loadingMC));
			loadingMC.x=this.x;
			loadingMC.y=this.y;
		}
		/**
		 * 销毁数据
		 */ 
		public function dispose():void
		{
			MemoryRecovery.getInstance().gcFun(loader,QueueLoaderEvent.QUEUE_PROGRESS,progress_handler);
			MemoryRecovery.getInstance().gcFun(loader,QueueLoaderEvent.QUEUE_COMPLETE,on_all_loaded);
			this.text=null;
			if(loader!=null)
			{
				loader.dispose();
				loader=null;
			}
			if(loadingMC!=null)
			{
				if(loadingMC.parent!=null)
				{
					loadingMC.parent.removeChild(loadingMC);
				}
				loadingMC.dispose();
				loadingMC=null;
			}
			this.container=null;
		}
	}
}